package com.example.exam.post.controller;

import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.db.PageInfoBean;
import com.example.exam.post.model.PostBean;
import com.example.exam.post.service.PostService;
import com.example.exam.user.model.UserRoleBean;
import com.example.exam.user.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/post")
public class PostController{
    private static String base_path="/post/";
    @Autowired
    PostService postService;
    @Autowired
    UserRoleService userRoleService;
    @GetMapping("/toPost")
        /**
            * @Description:去岗位信息页面
            * @Param: [request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toPost(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        /*List<UserRoleBean> list=userRoleService.authorization(userId);
        if(list!=null){
            for (int i=0;i<list.size();i++){
                //System.out.println("权限是"+list.get(i).getPre());
                if("SysPost|toEdit".equals(list.get(i).getPre())){
                    request.setAttribute("update",list.get(i));
                }else if("SysPost|toDelete".equals(list.get(i).getPre())){
                    request.setAttribute("delete",list.get(i));
                }else if("SysPost|toGrant".equals(list.get(i).getPre())){
                    request.setAttribute("grant",list.get(i));
                }else if ("SysPost|toAdd".equals(list.get(i).getPre())){
                    request.setAttribute("add",list.get(i));
                }
            }
        }*/
        return base_path+"post";
    }
    @GetMapping("/toAdd")
        /**
            * @Description:去添加岗位信息页面
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toAdd(){
        return base_path+"add";
    }
    @GetMapping("/toEdit")
        /**
            * @Description:去修改岗位信息页面
            * @Param: [id, request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toEdit(Integer id, HttpServletRequest request){
        PostBean postBean=postService.findById(id);
        request.setAttribute("postBean",postBean);
        return base_path+"edit";
    }
    @PostMapping("/add")
    @ResponseBody
        /**
            * @Description:添加岗位信息
            * @Param: [postBean]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean add(PostBean postBean){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=postService.addPost(postBean);
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/delete")
    @ResponseBody
        /**
            * @Description:删除岗位信息
            * @Param: [id]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean delete(Integer id){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=postService.delete(id);
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/edit")
    @ResponseBody
        /**
            * @Description:编辑岗位信息
            * @Param: [postBean]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean edit(PostBean postBean){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=postService.update(postBean);
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/list")
    @ResponseBody
        /** 
            * @Description:岗位信息列表
            * @Param: [page, limit, name] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean list(Integer page,Integer limit,String name){
        ResultBean resultBean = new ResultBean();
        try {
            PageInfoBean pageInfo = postService.list(page,limit,name);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            resultBean.fillData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return resultBean;
    }
    @RequestMapping("/toDelete")
    @ResponseBody
        /** 
            * @Description:  岗位信息批量删除
            * @Param: [request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean toDelete(HttpServletRequest request) {
        ResultBean result = new ResultBean();
        String role_id  = request.getParameter("id");
        try {
            Integer deleState = null;
            if(StringUtils.isNotEmpty(role_id)) {
                int[] roleIds = null;
                String[] ids = role_id.split(",");
                roleIds = new int[ids.length];
                for(int i=0;i<ids.length;i++){
                    roleIds[i]=Integer.parseInt(ids[i]);
                }
                for(int i = 0; i < roleIds.length; i++) {
                    deleState = postService.delete(roleIds[i]);
                }
                result.fillData(deleState);
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
            result.fillData(deleState);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
}
